﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace Primitive.Threading.Sync
{
   static class StrangeBehavior
    {
       //以后会讲到,将这个字段标记成volatile,就可以修正问题
       private static bool s_stopWorker = false;

       public static void Do()
       {
           Console.WriteLine("Main: letting worker run for 5 seconds");
           Thread t = new Thread(Work);
           t.Start();
           Thread.Sleep(5000);
           s_stopWorker = true;
           Console.WriteLine("Main: waiting for worker to stop");
           t.Join();
       }

       private static void Work()
       {
           int x = 0;
           while (!s_stopWorker)
           {
               x++;
               
           }
           Console.WriteLine("Worker: stopped when x = {0}", x);
       }
    }
}
